﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RENIT.DragNDropUpload.Extensibility;
using Microsoft.Office.Server.WebControls;
using Microsoft.SharePoint;
using RENIT.DragNDropUpload.Utils;
using Microsoft.SharePoint.WebPartPages;

namespace RENIT.DragNDropUpload.SP2010.DocumentSetsIntegration
{
    public class DocumentSetContentsWebPartAdapter :  IWebPartAdapter
    {
        #region IWebPartAdapter Members

        public bool IsWebPartSupported(System.Web.UI.WebControls.WebParts.WebPart webPart)
        {
            return webPart is DocumentSetContentsWebPart;
        }

        public Utils.UploadTargetData GetTargetData(System.Web.UI.WebControls.WebParts.WebPart webPart, IEnumerable<IWebPartAdapter> otherAdapters)
        {
            var part = webPart as DocumentSetContentsWebPart;
            var childPart = part.Controls.OfType<XsltListViewWebPart>().FirstOrDefault();
            Utils.UploadTargetData result = null;
            if (childPart != null)
            {
                var adapter = otherAdapters.FirstOrDefault(a => a.IsWebPartSupported(childPart));
                if (adapter != null)
                {
                    result = adapter.GetTargetData(childPart, otherAdapters);
                    result.WPCellID = "MSOZoneCell_WebPart" + part.Qualifier;
                }
            }
            return result;
        }

        #endregion
    }
}
